package april.fastjson.namefilter;

import com.alibaba.fastjson.serializer.JSONSerializer;
import com.alibaba.fastjson.serializer.NameFilter;
import com.alibaba.fastjson.serializer.SerializeWriter;

public class Bean {
	private int id;
	private String name;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public static void main(String[] args) {
		NameFilter filter = new NameFilter() {

			@Override
			public String process(Object source, String name, Object value) {
				if (name.equals("id")) {
					return "ID";
				}

				return name;
			}
		};

		SerializeWriter out = new SerializeWriter();
		JSONSerializer serializer = new JSONSerializer(out);
		serializer.getNameFilters().add(filter);

		Bean bean = new Bean();
		bean.setId(0);
		bean.setName("andy");
		serializer.write(bean);
		String text = out.toString();
		System.out.println(text);
	}
}
